import { closureOrder, refundWithOrder } from '@/api/modules/shop'
import { usePromise } from '@/hooks/core/usePromise'
import common from '@/utils/common'
import { number } from 'vue-types'
export function useOrder() {
  const { to } = usePromise()
  const orderStatusOptions = [
    { value: null, label: '全部' },
    { value: 0, label: '待付款' },
    { value: 1, label: '付款中' },
    { value: 10, label: '待发货' },
    { value: 20, label: '待收货' },
    { value: 50, label: '已完成' },
    { value: 65, label: '退款中' },
    { value: 68, label: '部分退款' },
    { value: 70, label: '已退款' },
    { value: 99, label: '已关闭' },
  ]
  const orderStatusDic: { [key: number]: string } = {
    0: '待付款',
    1: '付款中',
    10: '待发货',
    20: '待收货',
    50: '已完成',
    65: '退款中',
    68: '部分退款',
    70: '已退款',
    99: '已关闭',
  }

  function mergeStatus(status: number): number {
    switch (status) {
      case 1:
        return 0
      case 68:
        return 65
      default:
        return status
    }
  }

  function checkStatus(status: number, text: string | Array<string>): Boolean {
    if (status || status === 0) {
      const tmpStatus = mergeStatus(status)
      if (Array.isArray(text)) {
        return !!text.find((txt) => txt === orderStatusDic[tmpStatus])
      } else {
        return orderStatusDic[tmpStatus] === text
      }
    } else {
      console.log('请传入数据')
      return false
    }
  }

  function statusSteps(orderStatus: number) {
    if ([65, 68, 70].includes(orderStatus)) {
      return ['提交订单', '买家付款', '退款中', '退款成功']
    } else if (orderStatus === 99) {
      return ['提交订单', '关闭订单']
    } else {
      return ['提交订单', '买家付款', '卖家发货', '确认收货']
    }
  }

  function statusStepActive(orderStatus: number) {
    if (orderStatus === 0 || orderStatus === 1) {
      return 1
    } else if (orderStatus === 10) {
      return 2
    } else if (orderStatus === 20) {
      return 3
    } else if (orderStatus === 50) {
      return 4
    } else if (orderStatus === 65) {
      return 3
    } else if ([68, 70].includes(orderStatus)) {
      return 4
    } else if (orderStatus === 99) {
      return 2
    } else {
      return 0
    }
  }

  function orderStatusWithLabel(key: number) {
    // @ts-ignore
    if (key || key === 0) return orderStatusDic[key]
    return '没有状态'
  }
  /**
   * 结束订单
   * @param id  订单id
   */
  function closeOrderWithId(id: number) {
    return new Promise((resolve, reject) => {
      common
        .syncAlert('是否关闭订单')
        .then(async () => {
          const [err, res] = await to(closureOrder({ id }))
          if (err) {
            common.XmMessage(1, '结束订单失败失败')
            reject(err)
          } else {
            console.log('closeOrderWithId')
            resolve(res)
          }
        })
        .catch((err) => {
          reject(err)
        })
    })
  }

  /**
   * 退款
   * @param id 订单id
   * @param reason
   */
  function refund(id: number, reason?: string) {
    return new Promise((resolve, reject) => {
      common
        .syncAlert('是否确定退款。')
        .then(async () => {
          const [err, res] = await to(refundWithOrder({ id }))
          if (err) {
            common.XmMessage(1, '退款失败')
            reject(err)
          } else {
            resolve(res)
          }
        })
        .catch(() => {
          reject()
        })
      // const b = await common.syncAlert('是否确定退款。')
      // if (!b) reject()
    })
  }

  return {
    closeOrderWithId,
    refund,
    orderStatusOptions,
    orderStatusWithLabel,
    statusSteps,
    statusStepActive,
    checkStatus,
    orderStatusDic,
  }
}
